var div = document.getElementsByClassName("jianjian-headsizebar")[0];
var input = div.children[9].children[0];
// console.log(input);

input.addEventListener('click', function () {
    var div2 = document.getElementsByClassName("jianjianhead-sizeyc")[0]
    // console.log(div2);
    div2.classList.add('show');
});

var close = document.getElementById('close');
close.addEventListener('click', function () {
    var div2 = document.getElementsByClassName("jianjianhead-sizeyc")[0]
    div2.classList.remove('show');
});

//点击跳转登录界面
function to_login() {
    location.href = '/login.html';
}
//点击跳转购物车界面
function go_cart() {
    location.href = '/cart.html';
}

//用户
let user_login_msg = document.querySelector('.jianjian-yonghu>.jianjian-yonghuyc');
// console.log(user_login_msg);

//购物车
let user_cart_msg = document.querySelector('.jianjian-gouwuche>.jianjian-gouwucheyc');

// 判断是否登录
//创建用户数据对象
let user_data = {
    uid: '',
    uname: '',
};
//判断保存的uid是否为空
if (localStorage.getItem('uid')) {
    //不为空则保存到用户对象
    user_data.uid = localStorage.getItem('uid');
    user_data.uname = localStorage.getItem('uname');
}
//头部用户数据的渲染
user_login_msg.innerHTML = template('head-login', user_data);
// 获取购物车数据
get_cart(user_data.uid);

//退出登录
function back() {
    // 删除所有保存的数据
    localStorage.removeItem('uid');
    localStorage.removeItem("uname");
    localStorage.removeItem("address");
    localStorage.removeItem("allPrice");
    localStorage.removeItem("orderTime");
    location.reload();
}

//获取购物车数据
function get_cart(uid) {
    uid = uid ? uid : 0;
    ajax({
        url: '/pro/cart_detail',
        type: 'get',
        data: uid,
        success: function (data) {
            data = JSON.parse(data);
            //新建购物车数据对象
            let cart_data = {
                data: '',
                allPnum: 0,
                allPrice: 0.00,
            };

            // 数据不为空
            if (data != null) {
                let allPrice = 0;
                // 对商品列表进行遍历
                for (let i = 0; i < data.length; i++) {
                    //将每个商品价格乘上数量，累加进总价
                    allPrice += parseInt(data[i].price)*parseInt(data[i].pnum);
                }
                allPrice = allPrice.toFixed(2);

                //将加工过的数据保存到对象中
                cart_data = {
                    data: data,
                    allPnum: data.length,
                    allPrice: allPrice,
                }
            }
            //进行模板引擎渲染头部
            let str = template('head-cart', cart_data);
            user_cart_msg.innerHTML = str;
        },
        error: function (e) {
            console.log(e);
        }
    });
}


//根据购物车id删除商品
function cart_delete(id) {
    if (confirm('确定删除该商品吗？')) {
        ajax({
            url: '/pro/cart_delete',
            type: 'get',
            data: id,
            success: function (data) {
                alert('删除成功！');
                location.reload();
            },
            error: function (e) {
                console.log(e);
            }
        });
    }
}